import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'Home',
    meta: {
      title: '首页',
      roles: ['admin', 'vehicleAuditor', 'vehicleManager', 'visitManager', 'appuser'],
      icon: 'dashboard'
    },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: '首页',
          icon: 'dashboard',
          roles: ['admin', 'vehicleAuditor', 'vehicleManager', 'visitManager', 'appuser']
        }
      }
    ]
  },
]

export const asyncRoutes = [
  { path: "*", redirect: "/404", hidden: true },
  {
    path: '/long',
    component: Layout,
    redirect: '/long/index',
    name: 'long',
    meta: {
      title: '长期车辆管理',
      icon: 'el-icon-help',
      roles: ['admin', 'vehicleAuditor', 'vehicleManager']
    },
    children: [
      {
        path: '/long/index',
        name: 'Index',
        component: () => import('@/views/interiorVehicle/index'),
        meta: {
          title: '车辆管理',
          roles: ['admin', 'vehicleAuditor', 'vehicleManager']
        }
      },
      {
        path: '/long/record',
        name: 'Record',
        component: () => import('@/views/interiorVehicle/record'),
        meta: {
          title: '出入记录查询',
          roles: ['admin', 'vehicleAuditor', 'vehicleManager']
        }
      },
      {
        path: '/long/appproval',
        name: 'Approval',
        component: () => import('@/views/interiorVehicle/approval'),
        meta: {
          title: '车辆审批',
          roles: ['admin', 'vehicleAuditor']
        }
      }
    ]
  },
  {
    path: '/temporary',
    component: Layout,
    redirect: '/temporary/index',
    name: 'temporary',
    meta: {
      title: '临时车辆管理',
      icon: 'el-icon-document',
      roles: ['admin', 'vehicleAuditor', 'vehicleManager']
    },
    children: [
      {
        path: '/temporary/index',
        name: 'IndexTemp',
        component: () => import('@/views/temporaryVehicles/index'),
        meta: {
          title: '登记列表',
          roles: ['admin', 'vehicleAuditor', 'vehicleManager']
        }
      },
      {
        path: '/temporary/record',
        name: 'RecordTemp',
        component: () => import('@/views/temporaryVehicles/record'),
        meta: {
          title: '出入记录查询',
          roles: ['admin', 'vehicleAuditor', 'vehicleManager']
        }
      }
    ]
  },

  {
    path: '/visitor',
    component: Layout,
    redirect: '/visitor/index',
    name: 'visitor',
    meta: {
      title: '访客管理',
      icon: 'el-icon-user',
      roles: ['admin', 'visitManager']
    },
    children: [
      {
        path: '/visitor/index',
        name: 'IndexVisitor',
        component: () => import('@/views/visitor/index'),
        meta: {
          title: '登记列表',
          roles: ['admin', 'visitManager']
        }
      },
      {
        path: '/visitor/personRecord',
        name: 'PersonRecordVisitor',
        component: () => import('@/views/visitor/person-record'),
        meta: {
          title: '访客人员行踪查询',
          roles: ['admin', 'visitManager']
        }
      },
      {
        path: '/visitor/carRecord',
        name: 'CarRecordVisitor',
        component: () => import('@/views/visitor/car-record'),
        meta: {
          title: '访客车辆出入查询',
          roles: ['admin', 'visitManager']
        }
      }
    ]
  },

  {
    path: '/settings',
    component: Layout,
    redirect: '/settings/user',
    name: 'Settings',
    meta: {
      title: '系统管理',
      icon: 'el-icon-s-tools',
      roles: ['admin']
    },
    children: [
      {
        path: '/settings/user',
        name: 'User',
        component: () => import('@/views/settings/user/index'),
        meta: {
          title: '用户管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/role',
        name: 'Role',
        component: () => import('@/views/settings/role/index'),
        meta: {
          title: '角色管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/device',
        name: 'Device',
        component: () => import('@/views/settings/device/index'),
        meta: {
          title: '设备管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/process',
        name: 'Process',
        component: () => import('@/views/settings/process/index'),
        meta: {
          title: '审批流程管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/deptManager',
        name: 'DeptManager',
        component: () => import('@/views/settings/process/leader'),
        meta: {
          title: '审批人员管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/operLogManager',
        name: 'operLogManager',
        component: () => import('@/views/log/index'),
        meta: {
          title: '操作日志管理',
          roles: ['admin']
        }
      },
      {
        path: '/settings/dataManager',
        name: 'dataManager',
        component: () => import('@/views/database/index'),
        meta: {
          title: '数据库备份管理',
          roles: ['admin']
        }
      },
    ]
  },
  /*
  {
    path: '/test',
    component: Layout,
    redirect: '/test/vehicle',
    name: 'Test',
    meta: {
      title: '模拟测试',
      icon: 'el-icon-finished',
      roles: ['admin']
    },
    children: [
      {
        path: '/test/vehicle',
        name: 'VehicleTest',
        component: () => import('@/views/test/vehicle/index'),
        meta: {
          title: '车辆出入模拟',
          roles: ['admin']
        }
      },
      {
        path: '/test/guest',
        name: 'GuestTest',
        component: () => import('@/views/test/guest/index'),
        meta: {
          title: '访客出入模拟',
          roles: ['admin']
        }
      }
    ]
  },*/
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
